package homepage

import (
	"github.com/gin-gonic/gin"

	"go-papers/models"
	"go-papers/tools"
	"go-papers/tools/app"
)

func GetSearchList(c *gin.Context) {
	var data models.Course
	var err error
	var pageSize = 20
	var pageIndex = 1

	if size := c.Request.FormValue("pageSize"); size != "" {
		pageSize = tools.StrToInt(err, size)
	}
	if index := c.Request.FormValue("pageIndex"); index != "" {
		pageIndex = tools.StrToInt(err, index)
	}

	data.CourseStatus = "5"
	ss := models.CourseSearch{}
	ss.KeyWord = c.Request.FormValue("keyword")

	ss.MemberId = tools.GetMemberId(c)
	ss.OpenId = c.Request.FormValue("openId")
	ss.LocationKm = c.Request.FormValue("locationKm")

	//
	var third models.ThirdUser
	ss.ThirdUserId, _ = third.GetIDByOpenId(ss.OpenId)

	result, count, err := data.GetPageSearch(pageSize, pageIndex, ss)
	if err != nil {
		tools.HasError(err, "网络异常，请稍后重试"+err.Error(), -1)
	}

	app.PageOK(c, result, count, pageIndex, pageSize, "")
}

func GetRecommendList(c *gin.Context) {
	var data models.Course
	var err error
	var pageSize = 20
	var pageIndex = 1

	if size := c.Request.FormValue("pageSize"); size != "" {
		pageSize = tools.StrToInt(err, size)
	}
	if index := c.Request.FormValue("pageIndex"); index != "" {
		pageIndex = tools.StrToInt(err, index)
	}
	data.CourseStatus = "5"
	data.CourseName = c.Request.FormValue("courseName")
	data.CourseType = c.Request.FormValue("courseType")
	data.CourseCategory = c.Request.FormValue("courseCategory")
	data.OrganName = c.Request.FormValue("organName")
	data.BrandName = c.Request.FormValue("brandName")
	data.CourseTeach = c.Request.FormValue("courseTeach")
	if index := c.Request.FormValue("coursePrice"); index != "" {
		data.CoursePrice = tools.StringToFloat64(err, index)
	}
	if index := c.Request.FormValue("discountPrice"); index != "" {
		data.DiscountPrice = tools.StringToFloat64(err, index)
	}
	data.CourseTime = c.Request.FormValue("courseTime")
	data.CourseAge = c.Request.FormValue("courseAge")

	ss := models.CourseSearch{}
	ss.KeyWord = c.Request.FormValue("keyword")

	ss.MemberId = tools.GetMemberId(c)
	ss.OpenId = c.Request.FormValue("openId")
	ss.LocationKm = c.Request.FormValue("locationKm")

	//
	var third models.ThirdUser
	ss.ThirdUserId, _ = third.GetIDByOpenId(ss.OpenId)

	result, count, err := data.GetPageSearch(pageSize, pageIndex, ss)
	if err != nil {
		tools.HasError(err, "网络异常，请稍后重试"+err.Error(), -1)
	}
	app.PageOK(c, result, count, pageIndex, pageSize, "")
}
